#include "shimmingitem.h"

ShimmingItem::ShimmingItem(QWidget *parent)
    : QWidget{parent}
{
    setAttribute(Qt::WA_StyledBackground, true);
}

void ShimmingItem::SetShowTriangle(bool showTriangle)
{
    m_showTriangle = showTriangle;
    this->update();
}

void ShimmingItem::SetHighlight(bool highlight)
{
    m_highlight = highlight;
    this->update();
}

void ShimmingItem::paintEvent(QPaintEvent* event)
{
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
    DrawRetangular(painter);
    DrawTriangle(painter);
    DrawCellCount(painter);
}

void ShimmingItem::DrawRetangular(QPainter& painter)
{
    painter.save();

    QPen pen(QColor(0xff003868));
    pen.setWidth(1);
    painter.setPen(pen);
    if (m_highlight) {
        QLinearGradient gradient(0, 0, 0, height());
        gradient.setColorAt(0, QColor("#d9f6ff"));
        gradient.setColorAt(1, QColor("#4dcbff"));
        painter.setBrush(gradient);
    }
    painter.drawRect(rect());

    painter.restore();
}

void ShimmingItem::DrawTriangle(QPainter& painter)
{
    if (m_showTriangle) {
        painter.save();
        painter.setPen(Qt::NoPen);
        painter.setBrush(QColor(0xff009cdd));
        QPolygon polygon;
        polygon.setPoints(3, 1, 1, TRIANGLE_WIDTH + 1, 1, 1, TRIANGLE_WIDTH + 1);
        painter.drawPolygon(polygon);
        painter.restore();
    }
}

void ShimmingItem::DrawCellCount(QPainter& painter)
{
    painter.save();
    QFont font;
    font.setFamilies({"Noto Sans", "sans-serif"});
    font.setPixelSize(22);
    painter.setFont(font);
    painter.setPen(QColor("#003868"));
    if (m_loadType == Shimming::COUNT) {
        painter.drawText(rect(), Qt::AlignCenter, QString(QString::number(m_cellCount)));
    } else if (m_loadType == Shimming::RATIO) {
        painter.drawText(rect(), Qt::AlignCenter, QString(QString::number(m_cellRatio)));
    }
    painter.restore();
}

Shimming::LoadType ShimmingItem::m_loadType = Shimming::NONE;
